<template>
  <div class="manageGroup-page">
    <!-- 顶部筛选条件 -->
    <div class="conditions-container">
      <ScreenConditions :isDataType="false">
        <template slot="conditions">
          <!-- 项目名称 -->
          <bjSelect
            v-model="conditions.proName"
            allowClear
            :dataResource="conditions.proNameSource"
            placeholder="请选择项目名称"
            :loading="conditions.proNameLoading"
            :replaceFields="{ key: 'tenantCode', label: 'proName' }"
            @change="conditionChange($event)"
          ></bjSelect>
          <!-- 设备名称 -->
          <a-input
            v-model="conditions.groupName"
            allowClear
            placeholder="请输入群组名称"
          />
        </template>
        <template slot="operate">
          <a-button type="primary" @click="getList()"> 查询 </a-button>
        </template>
      </ScreenConditions>
    </div>

    <!-- 主体区域 -->
    <div class="result-container" v-loading="pageLoading">
      <div class="common-result-container-header" header="群组管理">
        <a-button type="primary" @click="gotoAdd()"> 新增 </a-button>
      </div>
      <div class="result-body">
        <!-- table -->
        <div class="table-container">
          <vxe-table
            align="center"
            :data="table.source"
            show-overflow
            show-header-overflow
            show-footer-overflow
            show-footer
            :column-config="{ resizable: true }"
            class="scrollbarStyle"
            height="99%"
            :sort-config="{ trigger: 'cell' }"
            ref="xTable"
            :scroll-x="{ gt: 10 }"
            :scroll-y="{ gt: 100 }"
            row-id="id"
            :checkbox-config="{
              reserve: true,
            }"
          >
            <vxe-column type="seq" width="80" title="序号"></vxe-column>
            <vxe-column
              :title="item.title"
              :field="item.key"
              :key="index"
              :min-width="item.width"
              show-overflow
              v-for="(item, index) in table.columns"
            >
            </vxe-column>
            <vxe-column title="操作" width="180" fixed="right" align="left">
              <template #default="{ row }">
                <div class="text-button-group">
                  <a-button type="link" @click="goEditor(row)"> 编辑 </a-button>
                  <a-button
                    type="link"
                    class="danger"
                    @click="handleDelClick(row)"
                  >
                    删除
                  </a-button>
                </div>
              </template>
            </vxe-column>
          </vxe-table>
        </div>
        <!-- 分页 -->
        <div class="pager-container">
          <vxe-pager
            background
            :current-page.sync="pager.page"
            :page-size.sync="pager.pageSize"
            :total="pager.total"
            :layouts="[
              'PrevPage',
              'JumpNumber',
              'NextPage',
              'Sizes',
              'FullJump',
            ]"
            @page-change="paginationChange"
          >
            <template #left>
              <div class="vxe-pager-left-tip-container">
                第<span>1</span>到<span
                  >{{ Math.ceil(pager.total / pager.pageSize) || 1 }}页 </span
                >共<span>{{ pager.total }}</span
                >条记录
              </div>
            </template>
          </vxe-pager>
        </div>
      </div>
    </div>
    <!-- 新增 -->
    <addModal :visible.sync="addVisible"></addModal>
    <!-- 编辑 -->
    <editorModal
      :visible.sync="editorVisible"
      :groupName="groupName"
      :userId="userId"
      :tenantCode="tenantCode"
      :id="id"
    ></editorModal>
  </div>
</template>

<script>
//【角色管理】列表
import bjSelect from "@/components/bj-select";
import { getLoginData } from "@/tools";
import { getProjectList } from "@/api/user-center/equipmentManage.js";
import { getList, deleteGroup } from "@/api/user-center/manageGroup.js";
import addModal from "./addModal.vue";
import editorModal from "./editorModal.vue";
export default {
  components: {
    bjSelect,
    addModal,
    editorModal,
  },
  data() {
    return {
      id: "",
      addVisible: false,
      editorVisible: false,
      pageLoading: false,
      //顶部筛选条件
      conditions: {
        //项目名称
        proNameSource: [],
        proName: undefined,
        proNameLoading: true,
        groupName: undefined,
      },
      //table
      table: {
        source: [],
        columns: [
          {
            title: "项目名称",
            dataIndex: "tenantName",
            key: "tenantName",
            width: "",
          },
          {
            title: "群组名称",
            dataIndex: "groupName",
            key: "groupName",
            width: "",
          },
          {
            title: "群组成员",
            dataIndex: "userName",
            key: "userName",
            width: "",
          },
        ],
      },
      //分页
      pager: {
        page: 1,
        pageSize: 10,
        total: 0,
      },
      groupName: "",
      userId: "",
      tenantCode: "",
    };
  },
  mounted() {
    this.getProjectList();
    this.getList();
  },
  methods: {
    //【查询条件】监听查询条件切换
    conditionChange(ev) {
      this.conditions.proName = ev;
      this.pager.page = 1;
      this.getList();
    },

    //查询列表
    getList() {
      return new Promise((resolve, reject) => {
        let { proName, groupName } = this.conditions;
        let { page, pageSize } = this.pager;
        this.pageLoading = true;
        getList({
          tenantCode: proName?.key || "",
          groupName,
          page,
          pageSize,
        })
          .then((res) => {
            let { data } = res;
            this.table.source = data?.result?.records || [];
            resolve();
          })
          .catch((_) => {
            this.table.source = [];
            this.pager.total = 0;
            reject();
          })
          .finally((_) => {
            this.pageLoading = false;
          });
      });
    },

    //【查询条件】查询项目名称
    getProjectList() {
      this.conditions.proNameLoading = true;
      return new Promise((resolve, reject) => {
        let { userId } = getLoginData();
        getProjectList({ userId })
          .then((res) => {
            let { data } = res;
            this.conditions.proNameSource = data?.result || [];
            resolve();
          })
          .catch((_) => {
            this.conditions.proNameSource = [];
            reject();
          })
          .finally((_) => {
            this.conditions.proNameLoading = false;
          });
      });
    },

    //删除
    handleDelClick(row) {
      let that = this;
      let { id } = row;
      const h = this.$createElement;
      this.$confirm({
        title: "您确定删除所选内容?",
        content: "删除内容后,将无法恢复。",
        okText: "确定",
        okType: "primary",
        cancelText: "取消",
        centered: true,
        icon: () =>
          h("a-icon", {
            props: {
              type: "question-circle",
              theme: "filled",
            },
            style: "color:#FB5A5A",
          }),
        onOk() {
          return new Promise((resolve, reject) => {
            deleteGroup({ id })
              .then((res) => {
                let { data } = res;
                if (data?.result == "操作成功") {
                  that.$message.success("操作成功");
                } else {
                  that.$message.error(data?.result || "操作失败，请稍后重试！");
                }
                if (that.table.source.length == 1) {
                  that.pager.page =
                    that.pager.page > 1 ? that.pager.page - 1 : that.pager.page;
                }
                that.getList();
              })
              .catch((_) => {
                that.$message.error("操作失败，请稍后重试！");
                reject();
              })
              .finally((_) => {
                resolve();
              });
          });
        },
        onCancel() {},
      });
    },

    //监听分页切换
    paginationChange({ currentPage, pageSize }) {
      this.pager.page = currentPage;
      this.pager.pageSize = pageSize;
      this.getList();
    },

    //新增
    gotoAdd() {
      this.addVisible = true;
    },

    //编辑
    goEditor(row) {
      let { groupName, userId, tenantCode, id } = row;
      this.editorVisible = true;
      this.userId = userId || "";
      this.tenantCode = tenantCode || "";
      this.id = id || "";
      this.groupName = groupName;
    },
  },
};
</script>

<style lang="less" scoped>
.manageGroup-page {
  height: 100%;
  .flex();
  flex-direction: column;
  box-sizing: border-box;
  padding: 0 0 10px 10px;
  .conditions-container {
    flex-shrink: 0;
    /deep/.ant-input-suffix {
      right: 24px;
      margin-top: -5px;
    }
  }
  .result-container {
    .flex();
    flex-direction: column;
    flex: 1;
    .common-result-container-header {
      flex-shrink: 0;
    }
    .result-body {
      flex: 1;
      .flex();
      flex-direction: column;
      .table-container {
        flex: 1;
        .table-more-btns {
          color: var(--primary-color);
          margin-top: -20px;
          transform: translateY(-3px);
          margin-left: 20px;
          display: inline-block;
          cursor: pointer;
        }
        .status-text {
          background: #4bc964;
          border-radius: 4px;
          color: #fff;
          padding: 4px 15px;
          &.error {
            background: #ff3f4a;
          }
        }
      }
      .pager-container {
        flex-shrink: 0;
      }
    }
  }
}
</style>
